package com.mine;

import java.util.ArrayDeque;
import java.util.Queue;

public class TreeUtils {
    public static TreeNode createTree(TreeNode node, Queue<Integer> queue) {
        Integer value = queue.poll();
        if (value == null || value == 0) {
            return null;
        }
        node.value = value;
        node.left = createTree(new TreeNode(), queue);
        node.right = createTree(new TreeNode(), queue);
        return node;
    }

    public static void frontDump(TreeNode rootNode) {
        if (rootNode == null) {
            return;
        }
        System.out.println(rootNode.value);
        frontDump(rootNode.left);
        frontDump(rootNode.right);
    }

    public static void centerDump(TreeNode rootNode) {
        if (rootNode == null) {
            return;
        }
        centerDump(rootNode.left);
        System.out.println(rootNode.value);
        centerDump(rootNode.right);
    }

    public static void backDump(TreeNode rootNode) {
        if (rootNode == null) {
            return;
        }
        backDump(rootNode.left);
        backDump(rootNode.right);
        System.out.println(rootNode.value);
    }

    public static void levelDump(TreeNode node) {
        if (node == null) {
            return;
        }
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.offer(node);
        while (!queue.isEmpty()) {
            node = queue.poll();
            System.out.println(node.value);
            if (node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null) {
                queue.offer(node.right);
            }
        }
    }
}
